package MyGO.utils.FileUtils;

import MyGO.TableApplication;
import MyGO.controller.CheckDoctorViewController;
import MyGO.controller.TableViewController;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;

import java.io.IOException;

public class FXMLLoaderUtil {
    public static Pane loadWithControllerByStatusAndDocID(String fxmlPath, String filtStatus, String filtDoctorID) throws IOException {
        FXMLLoader loader = new FXMLLoader(TableApplication.class.getResource(fxmlPath));

        // 设置构造器工厂
        loader.setControllerFactory(param -> {
            if (param == TableViewController.class) {
                return new TableViewController(filtStatus, filtDoctorID, null);
            } else {
                try {
                    return param.getDeclaredConstructor().newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

        });

//        loader.setControllerFactory(param -> {
//            if (param == CheckDoctorViewController.class) {
//                return new CheckDoctorViewController(filtStatus, filtDoctorID);
//            } else {
//                try {
//                    return param.getDeclaredConstructor().newInstance();
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//            }
//
//        });

        return loader.load();
    }

    public static Pane loadWithControllerByStatus(String fxmlPath, String filtStatus) throws IOException {
        FXMLLoader loader = new FXMLLoader(TableApplication.class.getResource(fxmlPath));

        loader.setControllerFactory(param -> {
            if (param == TableViewController.class) {
                return new TableViewController(filtStatus);
            } else {
                try {
                    return param.getDeclaredConstructor().newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        return loader.load();
    }
}
